<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<?php
date_default_timezone_set('Australia/Melbourne');
echo $dateToday = date("Y_m_d");
$csvFolder = "./csvs/";
$recordFolder = "./records/";
$productFolder = "./products/";
$barCodeCsv = "products_v0.csv";


$recordCsv="recorder_".$dateToday.".csv";
$productCsv = "product_".$dateToday.".csv";
$imProductCsv = "product_im_".$dateToday.".csv";
$noimProductCsv = "product_no_im".$dateToday.".csv";
//$fileName="recorder.csv";

//$arrayShipping = array();
//$arrayPacking = array();

$arrayProduct = array();

$fl = fopen($recordFolder.$recordCsv,"r");
while(($lineData = fgetcsv($fl,0))!= FALSE){
	//$arrayShipping[]= $lineData[2];
	$arrProductTemp = explode(",",$lineData[1]);
	$arrayProduct= array_merge($arrayProduct,$arrProductTemp);
	array_push($arrayShipping,$lineData[2]);
	//$arrayPacking[] = $lineData[4];
	array_push($arrayPacking,$lineData[4]);
//echo $lineData[2];
	echo ".";
	}
/*
$arrayShippingResult = array_count_values($arrayShipping);

$arrayResultData = array();
$arrayResultData = array_fill(0,19,"0");

foreach($arrayShippingResult as $key => $value){
	switch($key){
		case("**RRAC60**"):$arrayResultData[1] = $value;break;
		case("**RRAC120**"):$arrayResultData[2] = $value;break;
		case("**RRAC180**"):$arrayResultData[3] = $value;break;
		case("**RST60**"):$arrayResultData[4] = $value;break;
		case("**RST12**"):$arrayResultData[5] = $value;break;
		case("**RST18**"):$arrayResultData[6] = $value;break;
		case("**REGLB**"):$arrayResultData[7] = $value;break;
		case("**REP500**"):$arrayResultData[8] = $value;break;
		case("**REP3KG**"):$arrayResultData[9] = $value;break;
		case("**REP5KG**"):$arrayResultData[10] = $value;break;
		case("**EXS500**"):$arrayResultData[11] = $value;break;
		case("**EXS3KG**"):$arrayResultData[12] = $value;break;
		case("**EXS5kG**"):$arrayResultData[13] = $value;break;
		case("**EXSE**"):$arrayResultData[14] = $value;break;
		case("**FAYA2**"):$arrayResultData[15] = $value;break;
		case("**FAYA3**"):$arrayResultData[16] = $value;break;
		case("**FAYA5**"):$arrayResultData[17] = $value;break;
		case("**FAYLO**"):$arrayResultData[18] = $value;break;
		}
	
	}

$arrayResultData[0] = date("Y-m-d");
$shipFileName="shippingmethod.csv";
$fl = fopen($shipFileName,"a");
fputcsv($fl,$arrayResultData);
fclose($fl);

//print_r($arrayResultData);
$arrayPackingResult = array_count_values($arrayPacking);

$arrayResultPackingData = array();
$arrayResultPackingData = array_fill(0,7,"0");

foreach($arrayPackingResult as $key => $value){
	switch($key){
		case("**FREEPK**"):$arrayResultPackingData[1] = $value;break;
		case("**PEB11**"):$arrayResultPackingData[2] = $value;break;
		case("**PEC5**"):$arrayResultPackingData[3] = $value;break;
		case("**PEDL**"):$arrayResultPackingData[4] = $value;break;
		case("**PEC4**"):$arrayResultPackingData[5] = $value;break;
		case("9312650900014"):$arrayResultPackingData[6] = $value;break;
		}
	
	}
$arrayResultPackingData[0] = date("Y-m-d");
$packFileName="packingmethod.csv";
$fl = fopen($packFileName,"a");
fputcsv($fl,$arrayResultPackingData);
fclose($fl);	
//print_r(array_count_values($arrayPacking));	

//print_r(array_count_values($arrayProduct));	
*/

//$productFileName = "product.csv";

$fl = fopen($productFolder.$productCsv,"a");
foreach(array_count_values($arrayProduct) as $key =>$value){
		
		$arrTemp = array();
		
		$arrTemp[0] = $key;
		$arrTemp[1]	= $value;
		
		fputcsv($fl,$arrTemp);	
	}
fclose($fl);

// product HTML for printing 

$fl = fopen($csvFolder.$barCodeCsv,"r");
$strBarcode = fread($fl,filesize($csvFolder.$barCodeCsv));
$arrBarcode = explode("\r",$strBarcode);
//var_dump($arrBarcode);
fclose($fl);

//get product from carcode 
$arrImport = array();
$arrNoImport = array();

$fl = fopen($productFolder.$productCsv,"r");

while(($lineData = fgetcsv($fl,0))!= FALSE){

	if(array_search($lineData[0],$arrBarcode)=== false){
		
		$fl2 = fopen($productFolder.$noimProductCsv,"a");
		fputcsv($fl2,$lineData);
		fclose($fl2);		
		}
	else{
		$fl3 = fopen($productFolder.$imProductCsv ,"a");
		fputcsv($fl3,$lineData);
		fclose($fl3);					
		}	

	}
fclose($fl);	
//match if YES  save in one array 

//if NOT another arry

//struce the data and get out into TXT 

//send emails 


//export the csv for uploading to eBay
echo "DONE"; 
?>

<?php
// multiple recipients
$to  = 'eco1@phonecollection.com.au';

// subject
$subject = '$dateToday CSV Ready for download';

// message
$message = '
<html>
<head>
  <title>$dateToday</title>
</head>
<body>
  <p>Able To Import</p>
	http://192.168.1.124/eb2/products/product_im_$dateToday.csv <br />
  <p>Bar Code Not In the System</p>
	http://192.168.1.124/eb2/products/product_no_im$dateToday.csv <br />  
  
  </body>
</html>
';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Norman <eco1@phonecollection.com.au>, Monica <office1@phonecollection.com.au>' . "\r\n";
$headers .= 'From: CSV File <do-not-reply@phonecollection.com.au>' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);
?>

</body>
</html>